package console

import (
	"bufio"
	"svn-deploy-go/lib"
	"fmt"
	"os"
	"path"
	"strings"
)

var reader = bufio.NewReader(os.Stdin)

func ReadLine() (out string) {
	out, _ = reader.ReadString('\n')
	out = strings.TrimSpace(out)
	return
}

func ReadInput(usage string, default_value string) (out string) {
	fmt.Printf("%s[%s]:", usage, default_value)
	out, _ = reader.ReadString('\n')
	out = strings.TrimSpace(out)
	if len(out) == 0 {
		out = default_value
	}
	return
}

func ReadSVNItem() lib.SVNItem {
	return lib.SVNItem{
		Name:      ReadInput("Name of SVN Entry", ""),
		URL:       ReadInput("URL of SVN Entry", ""),
		Username:  ReadInput("User of SVN Entry", ""),
		Password:  ReadInput("Password of SVN Entry", ""),
		LocalPath: ReadInput("Remote Path of SVN Entry", ""),
		SSH: lib.SSHItem{
			Host:       ReadInput("Host of SSH Entry", ""),
			Username:   ReadInput("User of SSH Entry", ""),
			Password:   ReadInput("Password of SSH Entry", ""),
			PrivateKey: ReadInput("PrivateKey Path of SSH Entry", ""),
		},
	}
}

func ChangeSVNItem(item *lib.SVNItem) {
	item.Name = ReadInput("Name of SVN Entry", item.Name)
	item.URL = ReadInput("URL of SVN Entry", item.URL)
	item.Username = ReadInput("User of SVN Entry", item.Username)
	item.Password = ReadInput("Password of SVN Entry", item.Password)
	item.LocalPath = ReadInput("Remote Path of SVN Entry", item.LocalPath)
	item.SSH.Host = ReadInput("Host of SSH Entry", item.SSH.Host)
	item.SSH.Username = ReadInput("User of SSH Entry", item.SSH.Username)
	item.SSH.Password = ReadInput("Password of SSH Entry", item.SSH.Password)
	item.SSH.PrivateKey = ReadInput("PrivateKey Path of SSH Entry", item.SSH.PrivateKey)
}

func ListSVNItem(conf *lib.Config) {
	for _, item := range conf.Items {
		fmt.Println(item.Name)
	}
}

func Add(d *lib.Deploy) {
	fmt.Println("Please add new SVNItem")
	item := ReadSVNItem()
	for {
		err := d.AddItem(item)
		if err != nil {
			lib.ErrLogger.Println(err)
			ChangeSVNItem(&item)
		} else {
			break
		}
	}

	fmt.Println("Item name[", item.Name, "] has been added.")
}

func Edit(d *lib.Deploy) {
	fmt.Println("Please enter a name of SVNItem")
	cmd := ReadLine()
	item, _, err := d.FindByName(cmd)
	if err != nil {
		lib.ErrLogger.Println(err)
		return
	}
	ChangeSVNItem(&item)
	for {
		err := d.EditByName(cmd, item)
		if err != nil {
			lib.ErrLogger.Println(err)
			ChangeSVNItem(&item)
		} else {
			break
		}
	}

	fmt.Println("Item name[", item.Name, "] has been changed.")
}

func Deploy(d *lib.Deploy) {
	fmt.Println("Please enter the name of SVNItem for deploy")
	cmd := ReadLine()
	err := d.DeployItem(cmd)
	if err != nil {
		lib.ErrLogger.Println(err)
		return
	}
}

func Help() {
	fmt.Println("enter 'help' show this")
	fmt.Println("enter 'list' show all svn item")
	fmt.Println("enter 'add' add a new svn item")
	fmt.Println("enter 'deploy' deploy a svn item")
	fmt.Println("enter 'edit' edit a svn item")
}

func StartConsole(conf *lib.Config) {
	temp_path := path.Join(path.Dir(os.Args[0]), "temp")
	if _, err := os.Stat(temp_path); err != nil && os.IsNotExist(err) {
		os.MkdirAll(temp_path, 0777)
	}
	d := lib.NewDeploy(conf, temp_path)
	Help()
	for {
		cmd := ReadLine()
		lib.InfoLogger.Println()
		switch cmd {
		case "list":
			ListSVNItem(conf)
		case "add":
			Add(d)
		case "deploy":
			Deploy(d)
		case "help":
			Help()
		case "edit":
			Edit(d)
		}
	}

}
